<template>
    <div class="reset_account_set_container">
        <el-form ref="resetForm" :model="formData" status-icon :rules="rules" label-width="100px"
                 class="demo-ruleForm" style="padding-top: 18px;">
            <el-alert type="error" title="是否继续重置？？？一旦重置，原有数据永久丢失啊！！！"
                      :closable="false" style="margin-bottom: 10px;" />
            <el-form-item label="账套" prop="code">
                <el-input v-model="formData.code" :disabled="true"></el-input>
            </el-form-item>
            <el-form-item label="当前用户密码" prop="password">
                <el-input type="password" v-model="formData.password"></el-input>
            </el-form-item>
            <el-alert v-if="is_doing" :closable="false"
                      :title="accountSetState" :type="accountSetStateType">
            </el-alert>
        </el-form>
    </div>
</template>
<script>
import md5 from 'js-md5';

export default {
    name: 'ResetAccountSet',
    props: {
        propsData: {
            type: Object,
            default() {
                return {
                    init: true,
                };
            },
        },
    },
    data() {
        return {
            rules: {
                password: [
                    { required: true, message: '请输入密码', trigger: 'blur' }, ],
            },
            formData: {},
            //账套状态
            accountSetState: '',
            accountSetStateType: undefined,
            is_doing: false,
            timeout_handler: undefined,
        };
    },
    watch: {
        propsData: {
            handler() {
                this.init();
            },
            deep: true,
            immediate: true,
        },
    },
    beforeDestroy () {
        if (this.timeout_handler) {
            window.clearTimeout(this.timeout_handler);
        }
    },
    methods: {
        init() {
            const _this = this;
            if (_this.propsData.init) {
                return;
            }
            if (_this.$refs.resetForm) {
                _this.$refs.resetForm.clearValidate();
            }
            _this.formData = $.extend({}, _this.propsData.row);
            _this.checkAccountState({ sid: _this.formData.sid }, true);
        },
        checkAccountState(params, init) {
            const _this = this;
            return _this.fetchData(_this.$api.API_GetSysAccountSetBySid, params).then(result => {
                let resData = result.data || {};
                if (resData.state === 2) {
                    _this.accountSetState = '重置账套成功'
                    _this.accountSetStateType = 'success'
                    if (init) {
                        // this.is_doing = false;
                    }
                } else if (resData.state === 1) {
                    this.is_doing = true;
                    _this.accountSetState = '正在重置账套，已执行：' + resData.execute_duration_sec + "秒"
                    _this.accountSetStateType = 'warning'
                    _this.timeout_handler = window.setTimeout(() => {
                        _this.checkAccountState(params, init)
                    }, 3000);
                } else if (resData.state === -1) {
                    _this.accountSetState = '重置账套执行异常：' + resData.flyway_except
                    _this.accountSetStateType = 'warning'
                } else if (resData.state === 0) {
                    _this.accountSetState = '等待重置中，请耐心等候！'
                    _this.accountSetStateType = 'warning'
                }
            }).catch(res => {
                _this.accountSetState = '查询账套账套失败：' + res.message
                _this.accountSetStateType = 'error'
            });
        },
        async confirm() {
            const _this = this;
            let valid = await _this.validateForm('resetForm');
            if (valid) {
                let subData = $.extend({}, _this.formData);
                subData.password = md5(subData.password);
                return _this.fetchData(_this.$api.API_CreateDataBase, subData).then(() => {
                    _this.$message.success('开始重置！');
                    _this.accountSetState = '正在重置账套'
                    _this.accountSetStateType = 'info'
                    _this.checkAccountState({ sid: _this.formData.sid });
                    return false;
                }).catch(() => {
                    _this.checkAccountState({ sid: _this.formData.sid });
                    return false;
                });
            }
            return valid;
        },
    },
};
</script>
<style lang="less" rel="stylesheet/less">
.reset_account_set_container {
    width: 100%;
    height: 100%;
    overflow: hidden;
}
</style>
